package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type EquipmentHandler struct {
	svc *service.EquipmentService
}

func NewEquipmentHandler(svc *service.EquipmentService) *EquipmentHandler {
	return &EquipmentHandler{
		svc: svc,
	}
}

func (c *EquipmentHandler) RegistryRoutes(server *gin.Engine) {
	cg := server.Group("/equipment")
	cg.POST("/query", c.Query)
	cg.POST("/select", c.Select) // Equipments select
}

func (c *EquipmentHandler) Query(ctx *gin.Context) {
	type EquipmentReq struct {
		Name     string `json:"name"`
		Location string `json:"location"`
	}
	var req EquipmentReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}

	Equipments, err := c.svc.Query(ctx, domain.Equipment{
		Name:         req.Name,
		StoragePlace: req.Location,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Equipments)
}

func (c *EquipmentHandler) Select(ctx *gin.Context) {
	Equipments, err := c.svc.Select(ctx)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Equipments)
}
